package com.zhss.dfs.server;

import com.zhss.dfs.server.model.FileReplicaTask;

import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 用来描述datanode的信息
 * @author zhonghuashishan
 *
 */
public class DataNodeInfo {

	private String ip;
	private String hostname;
	private Integer port;
	/**
	 *  最近一次与datanode的心跳时间
	 */
	private Long latestHeatBeatTime;

	/**
	 *  当前dataNode已经存储的文件大小
	 */
	private Long fileStoreSize = 0L;

	/**
	 *  需要进行拷贝的副本任务
	 */
	private ConcurrentLinkedQueue<FileReplicaTask> copyFileReplicaQueue;

	/**
	 *  需要进行删除的副本任务
	 */
	private ConcurrentLinkedQueue<FileReplicaTask> delFileReplicaQueue;
	
	public DataNodeInfo(String ip, String hostname, Integer port) {
		this.ip = ip;
		this.hostname = hostname;
		this.latestHeatBeatTime =  System.currentTimeMillis();
		this.port = port;
		this.copyFileReplicaQueue = new ConcurrentLinkedQueue<>();
		this.delFileReplicaQueue = new ConcurrentLinkedQueue<>();
	}

	public DataNodeInfo() {
	}

	public void addReplaceTask(FileReplicaTask fileReplicaTask) {
		copyFileReplicaQueue.offer(fileReplicaTask);
	}

	public FileReplicaTask getFileReplicaTask(){
		return copyFileReplicaQueue.poll();
	}

	public void addDelReplicaTask(FileReplicaTask fileReplicaTask) {
		delFileReplicaQueue.offer(fileReplicaTask);
	}

	public FileReplicaTask getDelFileReplicaTask(){
		return delFileReplicaQueue.poll();
	}

	public Long getFileStoreSize() {
		return fileStoreSize;
	}

	public void setFileStoreSize(Long fileStoreSize) {
		this.fileStoreSize = fileStoreSize;
	}

	public void addFileStoreSize(Long fileSize){
		this.fileStoreSize += fileSize;
	}
	
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public String getHostname() {
		return hostname;
	}
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public Long getLatestHeatBeatTime() {
		return latestHeatBeatTime;
	}

	public void setLatestHeatBeatTime(Long latestHeatBeatTime) {
		this.latestHeatBeatTime = latestHeatBeatTime;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		DataNodeInfo that = (DataNodeInfo) o;
		return hostname.equals(that.hostname);
	}

	@Override
	public int hashCode() {
		return Objects.hash(hostname);
	}
}
